<?php
namespace app\admin\controller; 
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
class Information extends Base {
	
	function addEditInformation(){
    	$infor_id = I('id');
        $type = $infor_id > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
		$goodstype = I('type');
        //ajax提交验证
        if ((I('is_ajax') == 1) && IS_POST) {
            // 数据验证
            $data = input('post.');
            $validate = \think\Loader::validate('Infor');
            if (!$validate->batch()->check($data)) {
                $error = $validate->getError();
                $error_msg = array_values($error);
                $return_arr = array(
                    'status' => -1,
                    'msg' => $error_msg[0],
                    'data' => $error,
                );
                $this->ajaxReturn($return_arr);
            }
			
			
            if ($type == 2) {
            	M('information')->where("id=$infor_id")->save($data);
            } else {
            	$data['addtime'] = time();
				$storeid = session('store_id');				
				$data['storeid'] = $storeid;	
				$data['isshow'] = 1;				
				M('information')->add($data);
                $infor_id = $insert_id = M('information')->getLastInsID();
            }
			
				
            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('admin/Information/informationlist')),
            );
            $this->ajaxReturn($return_arr);
        }
		
		
		$goodsid = I('GET.id', 0);
        $goodsInfo = Db::name('information')->where('id=' .$goodsid)->find();
		$this->assign('goodsInfo', $goodsInfo);  // 商品详情
             
    }
	
	public function showAjaxInforList(){
    	$storeid = session('store_id');
		
        $where = " storeid= $storeid "; // 搜索条件                
        if(I('isshow') !== '') {
        	$where = "$where and isshow = ".I('isshow');
		}                
		
		$types = I('type/d',0);
		if($types!=0){
			$where = "$where and type= $types ";
		}
		
        // 关键词搜索               
        $key_word = I('key_word') ? trim(I('key_word')) : '';
        if($key_word)
        {
            $where = "$where and gname like '%$key_word%'" ;
        }
        
        $count = M('goods')->where($where)->count();
        $Page  = new AjaxPage($count,10);

        $show = $Page->show();
		
		if(I('orderby1')!=''){
			$order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
		}
		else{
			$order_str = "addtime desc";
		}
        
        $goodsList = M('information')->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('goodsList',$goodsList);
        $this->assign('page',$show);// 赋值分页输出
    }
	
	
	
	
	//资讯
	public function aeinformation(){
		$this->addEditInformation();
       	return $this->fetch('information');
	}
	
	
	//资讯列表
	public function informationlist(){
		return $this->fetch();
	}
	
	//资讯ajax列表
	public function ajaxinformation(){
		$this->showAjaxInforList();
        return $this->fetch();
	}
	
	//删除资讯
	public function delinformation(){
		$ids = I('post.ids','');
		empty($ids) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！！",'data'  =>'']);
        $goods_ids = rtrim($ids,",");
		
		$res = Db::name("information")->whereIn('id',$goods_ids)->select();
		foreach($res as $k=>$v){
			if($v['goodstp']==1){
				 $this->ajaxReturn(['status' => -1,'msg' => $v['gname'].' 请在对应的项目引进里删除']);
			}
		}	
		
		M("information")->whereIn('id',$goods_ids)->delete();  //商品表
        $this->ajaxReturn(['status' => 1,'msg' => '操作成功','url'=>U("Admin/Information/informationlist")]);
	}
}

?>